package cn.bgs.service.impl;

import cn.bgs.mapper.MaintenanceMapper;
import cn.bgs.mapper.VisitorsRecordMapper;
import cn.bgs.pojo.VisitorsRecord;
import cn.bgs.service.IVisitorsRecordService;
import cn.bgs.vo.CellIntermediate;
import cn.bgs.vo.VisitorsrecordVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;


/**
 * <p>
 * 访客记录表 服务实现类
 * </p>
 *
 * @author cf
 * @since 2022-06-07
 */
@Service
public class VisitorsRecordServiceImpl extends ServiceImpl<VisitorsRecordMapper, VisitorsRecord> implements IVisitorsRecordService {
    @Autowired
    private MaintenanceMapper maintenanceMapper;
    @Autowired
    private VisitorsRecordMapper visitorsRecordMapper;
    @Override
    public int insertvisitorsrecord(CellIntermediate cellIntermediate) {
//        添加中间表
        maintenanceMapper.saveCellIntermediate(cellIntermediate);
        VisitorsRecord visitorsRecord =new VisitorsRecord();
        visitorsRecord.setUserId(cellIntermediate.getUserId());
        visitorsRecord.setMiddleId(cellIntermediate.getMiddleId());
        visitorsRecord.setVisitorsName(cellIntermediate.getVisitorsName());
        visitorsRecord.setVisitorsPhone(cellIntermediate.getVisitorsPhone());
        visitorsRecord.setVisitorsAge(cellIntermediate.getVisitorsAge());
        visitorsRecord.setAccessTime(cellIntermediate.getAccessTime());
        visitorsRecord.setEndTime(cellIntermediate.getEndTime());
        visitorsRecord.setState("生效中");
        LocalDateTime accessTime =cellIntermediate.getAccessTime();
        LocalDateTime endTime=cellIntermediate.getEndTime();
        long beginTime=accessTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant().toEpochMilli();
        long lastTime=endTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant().toEpochMilli();
        long fangwenTime=(lastTime/60000-beginTime/60000)/60;
        System.out.println(fangwenTime);
        Double ll=(double)fangwenTime;
        visitorsRecord.setDuration(ll);
        //获取系统默认时区
//        ZoneId zoneId = ZoneId.systemDefault();
        //时区的日期和时间
//        ZonedDateTime zonedDateTime = accessTime.atZone(zoneId);
        //获取时刻
//        Date date = Date.from(zonedDateTime.toInstant());
        //格式化LocalDateTime、Date
//        DateTimeFormatter localDateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        System.out.println("格式化后：localDateTime:" + localDateTimeFormat.format(accessTime) + "  Date:" + dateFormat.format(date));
//        String a=localDateTimeFormat.format(accessTime);
//        String b=localDateTimeFormat.format(endTime);
        return visitorsRecordMapper.insertvisitorsrecord(visitorsRecord);
    }
    @Autowired
    private VisitorsRecordMapper mapper;
    @Override
    public List<VisitorsRecord> findAll(@Param("id")Integer id, @Param("zhuangtai") String zhuangtai, @Param("name") String name) {
        return mapper.findAll(id,zhuangtai,name);
    }

    @Override
    public List<VisitorsRecord> findById(Integer id) {
        return mapper.findById(id);
    }

    @Override
    public List<VisitorsrecordVo> getVisitorsList(Integer id,String stratTime,String endTime) {
        return visitorsRecordMapper.getVisitorsList(id,stratTime,endTime);
    }
}
